package com.eshop.servlet;

import java.io.IOException;

import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.eshop.bean.AddressBean;
import com.eshop.bean.CartBean;

import com.eshop.bean.SongXiaoBean;
import com.eshop.cart.dao.UpDateCartDao;
import com.eshop.userorder.dao.SearchAddressDao;

public class UserOrderServlet extends HttpServlet {


	public UserOrderServlet() {
		super();
	}


	public void destroy() {
		super.destroy();
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		Integer usersId = (Integer)session.getAttribute("userId");
		
	
		String[] amountS = request.getParameterValues("sx");
		String[] idS = request.getParameterValues("jwq");
		int result = 0;
		int count = 0;
		
		if( amountS == null || amountS.length == 0)
		{
			response.sendRedirect(request.getContextPath()+"/PAGE/InterFace.jsp");
		}
		else
		{
			
			int  amountSlength = amountS.length;
			UpDateCartDao upDateCartDao = new UpDateCartDao();
			for(int i=0;i<amountSlength;i++)
			{	
			    upDateCartDao.upDateCart(amountS[i], idS[i]);
			}
			
			
			for(int i=0;i<amountSlength;i++)
			{
			    upDateCartDao.upDatestock(amountS[i], idS[i]);
			   
			}
			
			for(int i=0;i<amountSlength;i++)
			{	
			    count = Integer.valueOf(amountS[i]) * upDateCartDao.searchPrice(idS[i]); 
			    result =result+count;
			}
			
			
			
			
			SearchAddressDao searchAddressDao = new SearchAddressDao();
			
			ArrayList<AddressBean> searchAddressList = searchAddressDao.searchAddress(usersId);
			ArrayList<CartBean> searchCart = searchAddressDao.searchCart(usersId);
			
			
			
			
			SongXiaoBean songXiaoBean = new SongXiaoBean();
			
			songXiaoBean.setAddress(searchAddressList);
			songXiaoBean.setCart(searchCart);
			
			request.setAttribute("result", result);
			request.setAttribute("SongXiao", songXiaoBean);
			
			request.getRequestDispatcher("/PAGE/checkout.jsp").forward(request, response);
			
		}

		
	}

	
	public void init() throws ServletException {
		
	}

}
